package cfs.controller;

import cfs.databean.CFSTransaction;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.dao.DAOException;

import cfs.databean.Customer;
import cfs.databean.Fund;
import cfs.databean.FundPriceHistory;
import cfs.databean.Position;
import cfs.formbean.CustomerFundForm;
import cfs.helper.Helper;
import cfs.helper.TransactionTypes;
import cfs.model.CustomerDAO;
import cfs.model.FundDAO;
import cfs.model.FundPriceHistoryDAO;
import cfs.model.Model;
import cfs.model.PositionDAO;
import cfs.model.TransactionDAO;

public class ViewAccountAction extends Action {

	private CustomerDAO customerDAO;
	private FundDAO fundDAO;
	private TransactionDAO transactionDAO;
	private PositionDAO positionDAO;
	private FundPriceHistoryDAO fundPriceHistorDAO;

	public ViewAccountAction(Model model) {
		customerDAO = model.getCustomerDao();
		fundDAO = model.getFundDao();
		transactionDAO = model.getTransactionDao();
		positionDAO = model.getPositionDao();
		fundPriceHistorDAO = model.getFundHistoryDao();
	}

	public String getName() {
		return "viewAccount.do";
	}

	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);

		try {
                        int id = 0;
                        Customer user = new Customer();
                        if(request.getParameter("selectedId") != null){
                           String sid = (String) request.getParameter("selectedId");
                           id = Integer.parseInt(sid);
                        }else{
                            user = (Customer) request.getSession().getAttribute("user");
                            id = user.getCustomer_id();
                        }
                        user = customerDAO.lookup(id);
			// get last trading day
			Date lastTradingDay = null;
			lastTradingDay = transactionDAO.getLastTradingDay(user
					.getCustomer_id());

			// get customer's funds list
			Position[] pos = positionDAO.getFundsByCustomerId(user
					.getCustomer_id());
			List<CustomerFundForm> fundList = new ArrayList<CustomerFundForm>();

			for (int i = 0; i < pos.length; i++) {
				CustomerFundForm form = new CustomerFundForm();
				Fund fund = fundDAO.lookup(pos[i].getFund_id());
				FundPriceHistory priceHistory = fundPriceHistorDAO
						.getLastestPrice(pos[i].getFund_id());
				form.setName(fund.getName());
                                form.setFund_id(fund.getFund_id());
				form.setSymbol(fund.getSymbol());
				form.setShares(Helper.formatWith3Decimal((double) pos[i]
						.getShares() / 1000));
				if (priceHistory != null)
					form.setPrice("$"
							+ Helper.formatWith2Decimal((double) priceHistory
									.getPrice() / 100));
				else
					form.setPrice("N/A");
				fundList.add(form);
			}
			
			long aBalance = user.getAvailablebalance();
			long cash = user.getCash();

			request.setAttribute("availableBalance",
					Helper.longToDouble2String(aBalance));
			request.setAttribute("cash", Helper.longToDouble2String(cash));

			if (lastTradingDay == null)
				request.setAttribute("lastTradingDay", "N/A");
			else
				request.setAttribute("lastTradingDay", lastTradingDay);
			
			request.setAttribute("customer", user);
			request.setAttribute("fundList", fundList);

		} catch (DAOException e) {
			errors.add("Can not fetch the Last Trading Day");
                        return "viewAccount.jsp";
		}

		return "viewAccount.jsp";
	}
        
        
}
